<template>
	<!-- 器官设置 -->
	<div class="page-wrap">
		<div class="page-content p-0">
			<div class="page-column p-0">
				<div class="page-form p-x-0">
					<el-form
						:model="ruleForm"
						:rules="rules"
						label-position="left"
						label-width="154px"
						ref="form"
						label-suffix="："
					>
						<el-row>
							<el-col :span="24">
								<div class="my_title">器官计算选项</div>
								<el-form-item label="DCF文件" prop="DCF_FILE001">
									<select-para
										size="medium"
										class="mediumForm"
										@change="optionsChange"
										v-model="ruleForm.DCF_FILE001"
										:label.sync="ruleForm.DCF_FILE001"
										:options="DCF_FILE001options"
									></select-para>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row>
							<el-col :span="24">
								<div class="my_title m-b-20">器官计算选项</div>

								<el-table
									v-loading="loading"
									ref="multipleTable"
									:style="{'width':'70%','margin-left':'154px'}"
									:data="ruleForm.DISPERSIONtab"
									tooltip-effect="dark"
									border
								>
									<el-table-column prop="name" label="器官名称" align="center" min-width="120">
										<template slot-scope="scope">
											<div class>{{scope.row.name}}</div>
										</template>
									</el-table-column>
									<el-table-column prop="MIORGDEF_FLG" label="器官是否计算" align="center" min-width="120">
										<template slot-scope="scope">
											<el-form-item label-width="0px" prop="MIORGDEF_FLG">
												<select-para
													:disabled="disabled"
													style="width:100%"
													v-model="scope.row.MIORGDEF_FLG"
													:label.sync="scope.row.MIORGDEF_FLG"
													:options="weatherCalculate"
												></select-para>
											</el-form-item>
										</template>
									</el-table-column>
								</el-table>

								<el-form-item :style="{'margin-top':'20px'}" label="关键器官" prop="SRCRIORG001">
									<select-para
										size="medium"
										class="mediumForm"
										v-model="ruleForm.SRCRIORG001"
										:label.sync="ruleForm.SRCRIORG001Name"
										:options="SRCRIORG001options"
									></select-para>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
				</div>
				<div class="bottom-btn">
					<el-button
						type="primary"
						:disabled="this.basic_Data.OCENDAT1001=='TRUE'"
						:loading="loading"
						@click="submitFun"
					>下一步</el-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import selectPara from "@/components/selectPara"
import commCheckbox from "@/components/commCheckbox"
import ImageCropper from "@/components/ImageCropper"
import FixBottomButton from "@/components/FixBottomButton"
import ueditor from "@/components/ueditor"
import textLength from "@/components/common/comp/textLength"
import { commPara } from "@/mixins/mixins"
import { mapState } from "vuex"

export default {
	name: "aboutUs",
	props: {},
	mixins: [commPara],
	components: {
		selectPara,
		ImageCropper,
		textLength,
		commCheckbox,
		FixBottomButton,
		ueditor,
	},

	data() {
		return {
			activeTab: "3",
			ruleForm: {
				SRCRIORG001: "",
				SRCRIORG001Name: "",
				DCF_FILE001: "DOSD60.INP",
				MIORGDEF_ORG: [],
				DISPERSIONtab: [],
			},
			disabled: true,
			rules: {
				DCF_FILE001: [
					{
						required: true,
						message: "请选择",
						trigger: "change",
					},
				],
			},
			DCF_FILE001options: [
				{
					label: "DOSD60.INP",
					value: "DOSD60.INP",
				},
				{
					label: "DOSD825.INP",
					value: "DOSD825.INP",
				},
				{
					label: "DOSDATA.INP",
					value: "DOSDATA.INP",
				},
				{
					label: "DOSDATA.NEW",
					value: "DOSDATA.NEW",
				},
			],
			MIORGDEF_ORGoptions: [],
			SRCRIORG001options: [],
			MIORGDEF_ORG1: [],
			MIORGDEF_ORG1: [],
			weatherCalculate: [
				{
					label: "TRUE",
					value: "TRUE",
				},
				{
					label: "FALSE",
					value: "FALSE",
				},
			],

			loading: false,
		}
	},

	computed: {
		...mapState({
			early_Data: (state) => state.token.early_Data,
			basic_Data: (state) => state.token.basic_Data,
		}),
	},
	created() {
		this.getMIORGDEF_ORG1()
	},

	methods: {
		setData() {
			this.ruleForm.DCF_FILE001 = this.early_Data.DCF_FILE001
				? this.early_Data.DCF_FILE001
				: "DOSD60.INP"
			if (
				this.ruleForm.DCF_FILE001 == "DOSDATA.INP" ||
				this.ruleForm.DCF_FILE001 == "DOSDATA.NEW"
			) {
				this.disabled = false
				this.MIORGDEF_ORG = this.MIORGDEF_ORG1
				this.ruleForm.DISPERSIONtab = this.MIORGDEF_ORG1.map(
					(item, index) => {
						return {
							name: item.dic_cname,
							MIORGDEF_FLG: this.early_Data.MIORGDEF_FLG[index]
								? this.early_Data.MIORGDEF_FLG[index]
								: "TRUE",
						}
					}
				)
				this.SRCRIORG001options = this.MIORGDEF_ORG1.map((item) => {
					return {
						label: item.dic_cname,
						value: item.dic_ename,
					}
				})
				this.loading = false
			} else if (
				this.ruleForm.DCF_FILE001 == "DOSD60.INP" ||
				this.ruleForm.DCF_FILE001 == "DOSD825.INP"
			) {
				this.disabled = true
				// // console.log(this.early_Data)
				// console.log(this.MIORGDEF_ORG2)
				this.MIORGDEF_ORG = this.MIORGDEF_ORG2
				this.ruleForm.DISPERSIONtab = this.MIORGDEF_ORG1.map(
					(item, index) => {
						return {
							name: item.dic_cname,
							MIORGDEF_FLG: this.early_Data.MIORGDEF_FLG[index]
								? this.early_Data.MIORGDEF_FLG[index]
								: "FALSE",
						}
					}
				)
				this.SRCRIORG001options = this.MIORGDEF_ORG2.map((item) => {
					return {
						label: item.dic_cname,
						value: item.dic_ename,
					}
				})
				this.loading = false
			}
			this.ruleForm.SRCRIORG001 = this.early_Data.SRCRIORG001
				? this.early_Data.SRCRIORG001
				: ""
		},
		optionsChange(value) {
			// console.log(value)
			if (value == "DOSDATA.INP" || value == "DOSDATA.NEW") {
				this.disabled = false
				this.ruleForm.MIORGDEF_ORG = this.MIORGDEF_ORG1.map((item) => {
					return item.dic_cname
				})
				this.ruleForm.MIORGDEF_ORGE = this.MIORGDEF_ORG1.map((item) => {
					return item.dic_ename
				})
				this.SRCRIORG001options = this.MIORGDEF_ORG1.map((item) => {
					return {
						label: item.dic_cname,
						value: item.dic_ename,
					}
				})
			} else if (value == "DOSD60.INP" || value == "DOSD825.INP") {
				this.disabled = true
				this.ruleForm.MIORGDEF_ORG = this.MIORGDEF_ORG2.map((item) => {
					return item.dic_cname
				})
				this.ruleForm.MIORGDEF_ORGE = this.MIORGDEF_ORG2.map((item) => {
					return item.dic_ename
				})
				this.SRCRIORG001options = this.MIORGDEF_ORG2.map((item) => {
					return {
						label: item.dic_cname,
						value: item.dic_ename,
					}
				})
			}
		},
		getMIORGDEF_ORG2() {
			let params = {
				dictypeid: 2,
			}
			this.loading = true
			this.$appApi
				.ZD_GET_DICINFOS(params)
				.then((res) => {
					var str = res.data.substring(8)
					this.MIORGDEF_ORG2 = JSON.parse(str)
					this.loading = false
					this.setData()
				})
				.catch((err) => {
					console.log(err)
				})
		},
		getMIORGDEF_ORG1() {
			let params = {
				dictypeid: 1,
			}
			this.loading = true
			this.$appApi
				.ZD_GET_DICINFOS(params)
				.then((res) => {
					var str = res.data.substring(8)
					this.MIORGDEF_ORG1 = JSON.parse(str)
					this.getMIORGDEF_ORG2()
					this.loading = false
				})
				.catch((err) => {
					console.log(err)
				})
		},

		submitFun() {
			//保存
			this.$refs.form.validate((valid) => {
				if (valid) {
					let tempdata = JSON.parse(JSON.stringify(this.early_Data))
					tempdata.DCF_FILE001 = this.ruleForm.DCF_FILE001
					tempdata.MIORGDEF_ORG = this.ruleForm.MIORGDEF_ORGE
					tempdata.MIORGDEF_FLG = this.ruleForm.DISPERSIONtab.map(
						(item) => {
							return item.MIORGDEF_FLG
						}
					)
					tempdata.SRCRIORG001 = this.ruleForm.SRCRIORG001
					tempdata.SETPAGEINDEX = this.activeTab
					// console.log(tempdata)
					this.$store.dispatch("upData_early_Data", tempdata)
					let params = {
						batchid: this.$route.query.id,
						batchinfo: "ZAOQI",
						submitjson: JSON.stringify(tempdata),
					} 
					this.$appApi
						.BATCH_SUBMIT(params)
						.then((res) => { 
							var str = res.data.substring(0, 1)
							if (str == "E") {
								let message = res.data.substring(6)
								this.$notify.error({
									title: "保存失败！",
									message: message,
									type: "success",
								})
							} else if (str == "S") {
								this.$notify({
									message: "保存成功！",
									type: "success",
								})
							}
						})
						.catch((err) => {
							console.log(err)
						})
					this.$emit("changeActiveTab", this.activeTab)
					this.$store.dispatch("change_early_showTab", this.activeTab)
				}
			})
		},
	},
}
</script>
<style lang="scss" scoped>
.page-flex-row /deep/ .el-collapse-item__header {
	background: #f3f9fe;
}
.apparatusListItem {
	width: 550px;
	height: 150px;
	display: flex;
	justify-content: space-around;
	align-items: center;
	box-shadow: 0px 0px 5px #ccc;
	margin: 10px;
	padding: 20px;
	.apparatusItem {
		height: 110px;
	}
	i {
		font-size: 20px;
	}
}
</style>